<?php
/**
 * 职位管理
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/7/4
 * Time: 15:19
 */
namespace app\admin\controller;

use app\repository\Repository;
use think\Request;

class Jobs extends Common
{
    public function listUI()
    {
        return $this->fetch('list');
    }

    public function queryPage()
    {
        $rows = db('jobs')->select();

        $data = Repository::treeData($rows);

        return ['code'=>1,'data'=>$data];
    }


    /**
     * 添加故障
     * @param Request $request
     * @return \think\response\Json
     */
    public function add(Request $request){
        $input = $request->param();
        $row = db('jobs')->insertGetId($input);
        if($row){
            return json(['code'=>1,'msg'=>'添加成功']);
        }
        return json(['code'=>0,'msg'=>'添加失败']);
    }

    /**
     * 编辑故障
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function edit(Request $request){
        $input = $request->param();
        if(empty($input['id'])) return json(['code'=>0,'msg'=>'id参数错误']);
        $row = db('jobs')->where('id',$input['id'])->update($input);
        if($row){
            return json(['code'=>1,'msg'=>'修改成功']);
        }
        return json(['code'=>0,'msg'=>'修改失败']);
    }

    /**
     * 删除
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del(){
        $id = input('id');
        if(empty($id)) return json(['code'=>0,'msg'=>'id参数错误']);
        $count = db('jobs')->where('pid',$id)->count();
        if($count>0) return json(['code'=>0,'msg'=>'请先删除子故障']);
        $row = db('jobs')->where('id',$id)->delete();
        if($row){
            return json(['code'=>1,'msg'=>'删除成功']);
        }
        return json(['code'=>0,'msg'=>'删除失败']);
    }
}